package org.opentcs.kcvehicle.communication.kc.udp.Service;

import java.util.ArrayList;
import java.util.List;
import org.opentcs.kcvehicle.communication.kc.udp.agv.param.AgvEvent;
import org.opentcs.kcvehicle.communication.kc.udp.agv.param.AgvEventConstant;
import org.opentcs.kcvehicle.communication.kc.udp.agv.param.function.read.ReadParam;
import org.opentcs.kcvehicle.communication.kc.udp.agv.param.function.read.ReadRsp;
import org.opentcs.kcvehicle.communication.kc.udp.agv.param.function.read.ReadStrValue;
import org.opentcs.kcvehicle.communication.kc.udp.agv.param.function.read.ReadValueMember;
import org.opentcs.kcvehicle.communication.kc.udp.agv.param.rsp.RcvEventPackage;
import org.opentcs.kcvehicle.communication.kc.udp.io.UDPClient;

public class ReadValue extends BaseCommand{

  private static AgvEvent readValue() {
    AgvEvent agvEvent = new AgvEvent(AgvEventConstant.CommandCode_READ);
    List<ReadValueMember> readValueMemberList = new ArrayList<>();
    ReadValueMember readValueMember1 = new ReadValueMember(Short.valueOf("10"), Short.valueOf("1"));
    readValueMemberList.add(readValueMember1);

    List<ReadStrValue> readStrValueList = new ArrayList<>();
    ReadStrValue readStrValue = new ReadStrValue("Lift", 1, readValueMemberList);
    readStrValueList.add(readStrValue);

    ReadParam readParam = new ReadParam(agvEvent.getTransationId(), readStrValueList);
    agvEvent.setBody(readParam.toBytes());

    return agvEvent;
  }

  public static byte[] command() {
    AgvEvent agvEvent = readValue();
    printInfo(agvEvent);
    RcvEventPackage rcv = UDPClient.localAGV.send(agvEvent);
    if (rcv.isOk()) {
      System.out.println();
      System.out.println("-received readvalue" + "isok:" + rcv.isOk() + " dataBytes:");
      printInfo(rcv);
      ReadRsp readRsp = new ReadRsp(rcv.getDataBytes());
      return readRsp.dataValue;
    }
    else {
      System.out.println();
      System.out.println("-received transationId : " + "isok:" + rcv.isOk());
      throw new RuntimeException("-read failed");
    }
  }

}
